package com.sx.pipeline.ui.goods.detail

import android.annotation.SuppressLint
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ImageAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActGoodsDetailBinding
import com.sx.pipeline.ui.goods.GoodsVM
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.indicator.CircleIndicator


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 供应商-商品详情
 */
@Route(path = RoutePath.Mine.COMMODITY_DETAIL)
class GoodsDetailActivity :
    CYDataBindingBaseActivity<ActGoodsDetailBinding,GoodsVM>(R.layout.act_goods_detail), View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var goodsId: Int? = 0

    @JvmField
    @Autowired(name = IntentKey.ISEDIT)
    var isEdit: Int? = 0

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        if (isEdit==0){
            mBinding.tabEdit.visibility=View.VISIBLE
        }

    }

    override fun onResume() {
        super.onResume()
        //获取商品详情数据
        goodsId?.let { mViewModel.getGoodsDetail(it) }
    }

    @SuppressLint("SetTextI18n")
    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.goodsDetailDataEvent){
            it?.productImage?.split(",")?.let { it1 -> initBanner(it1) }
            it?.let {
                //商品名称
                mBinding.tvGoodsName.text=it.name
                mBinding.sbGoodsName.rightText=it.name
                //产品型号
                mBinding.tvGoodsModel.text="产品型号："+it.productModel
                mBinding.sbGoodsModel.rightText=it.productModel
                //价格
                mBinding.tvGoodsPrice.text="¥${it.price}"
                mBinding.sbPrice.rightText="${it.price}元"
                //主要参数
                mBinding.sbMainParameters.rightText=it.parameter
                //产品制造厂家
                mBinding.sbManufacturer.rightText=it.manufacturer
                //服务区域
                mBinding.sbServiceArea.rightText=it.addressDetail
                //商品销售单位
                mBinding.sbSalesUnit.rightText=it.unit
                //联系人
                mBinding.sbContact.rightText=it.contact
                //联系电话
                mBinding.sbContactNumber.rightText=it.contactPhone
                //销售分类
                mBinding.sbSalesClassification.rightText=it.salesClass
            }
        }
    }

    private fun initBanner(list:List<String>){
        mBinding.banner.setAdapter(ImageAdapter(list))
        mBinding.banner.isAutoLoop(false)
        mBinding.banner.setIndicator(CircleIndicator(this))
        mBinding.banner.setIndicatorGravity(IndicatorConfig.Direction.CENTER)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_edit->{
                //编辑资料
                goodsId?.let { RouteManager.Mine.intentToGoodsAdd(2, it) }
                finish()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        mBinding.banner.start()
    }

    override fun onStop() {
        super.onStop()
        mBinding.banner.stop()
    }
}